<template>
	<view>
		<button type="primary" @tap="payState()">已完成支付？</button>
	</view>
</template>

<script>
	import local_openid from '@/util/weixin/local-openid.js'
	import weixinpayapi from '@/util/weixin/weixinpay.js';
	import {
		request
	} from '@/api/request/index.js'
	import local_userinfo from '@/util/user/local-userinfo.js'
	//用户缓存信息
	let userSession = local_userinfo.getUserinfo();

	export default {
		data() {
			return {
				code: '',
				myopenid: '',
				orderid: ''
			};
		},
		onLoad(options) {
			let orderid = options.orderid
			if (!orderid) return uni.showModal({
				title: '警告',
				content: '缺少重要参数，请返回重试',
				showCancel: false
			})
			this.orderid = orderid
			this.initPaymentrequest();
			
			// #ifdef MP-WEIXIN
			uni.hideShareMenu()
			// #endif
		},
		methods: {
			initPaymentrequest() {
				let that = this;
				let channel_from = '';

				let openid = local_openid.getMyopenid();
				if (openid) this.myopenid = openid;

				// #ifdef MP-WEIXIN
				this.setWeixinpayMP();
				// #endif
				// #ifdef H5
				this.setWeixinpayH5();
				// #endif
			},
			//1
			setWeixinpayH5() {
				let that = this;

				weixinpayapi.setWeixinpay({
					orderid: that.orderid,
					channel_from: 'H5',
					openid: that.myopenid,
					callback: that.jsSdk
				});
			},
			//2
			jsSdk(params) {
				let _this = this;
				if (typeof window.WeixinJSBridge === 'undefined') {
					if (document.addEventListener) {
						document.addEventListener(
							'WeixinJSBridgeReady',
							function() {
								_this.onBridgeReady(params);
							},
							false
						);
					} else if (document.attachEvent) {
						document.attachEvent('WeixinJSBridgeReady', function() {
							_this.onBridgeReady(params);
						});
						document.attachEvent('onWeixinJSBridgeReady', function() {
							_this.onBridgeReady(params);
						});
					}
				} else {
					_this.onBridgeReady(params);
				}
			},
			//3
			onBridgeReady(params) {
				let _this = this;
				uni.showLoading({
					mask: true
				});
				window.WeixinJSBridge.invoke(
					'getBrandWCPayRequest', {
						appId: _this.$h5appid, // 公众号名称，由商户传入
						timeStamp: params.timeStamp, // 时间戳，自1970年以来的秒数
						nonceStr: params.nonceStr, // 随机串
						package: params.package,
						signType: params.signType, // 微信签名方式：
						paySign: params.paySign // 微信签名
					},
					function(res) {
						uni.hideLoading();
						let msg = '';
						if (res.err_msg == 'get_brand_wcpay_request:ok') {
							uni.showModal({
								title: "支付完成",
								showCancel: false,
								success(res) {
									if (res.confirm) uni.redirectTo({
										url: '../../pages/pay-success/pay-success?orderid=' + that.orderid
									});
								}
							});
						} else {
							uni.showModal({
								content: '已取消支付',
								showCancel: false
							});
						}
					}
				);
			},
			//1
			setWeixinpayMP() {
				let that = this;
				let go = function(tag) {
					uni.showLoading({
						mask: true
					});

					uni.requestPayment({
						provider: 'wxpay',
						timeStamp: tag.timeStamp,
						nonceStr: tag.nonceStr,
						package: tag.package,
						signType: tag.signType,
						paySign: tag.paySign,
						success(payres) {
							uni.showModal({
								title: "支付完成",
								showCancel: false,
								success(res) {
									if (res.confirm) uni.redirectTo({
										url: '../../pages/pay-success/pay-success?orderid=' + that.orderid
									});
								}
							});
						},
						fail(error) {
							console.log(error);
							uni.showModal({
								content: '已取消支付',
								showCancel: false
							});
						},
						complete() {
							uni.hideLoading();
						}
					});
				};

				weixinpayapi.setWeixinpay({
					orderid: that.orderid,
					openid: that.myopenid,
					channel_from: 'MP',
					callback: go
				});
			},
			toPay() {
				// #ifdef MP-WEIXIN
				this.setWeixinpayMP();
				// #endif
				// #ifdef H5
				this.setWeixinpayH5();
				// #endif
			},
			payState() {
				this.getOrderinfo();
			},
			getOrderinfo() {
				let that = this;
				uni.showLoading({
					mask: true
				});
				request.get({
						data: {
							c: 'AddPaymentController',
							m: 'GetOrderInfo',
							rediskey: userSession.sessionid,
							orderid: that.orderid
						}
					})
					.then(res => {
						if (res.Status == "ok") {
							uni.showModal({
								title: "订单未完成支付，是否继续支付？",
								confirmText: '继续支付',
								cancelText: '放弃支付',
								cancelColor: '#e64340',
								confirmColor: '#008000',
								success: (res) => {
									if (res.cancel) uni.reLaunch({
										url: '../../pages/tabBar/index'
									});
									if (res.confirm) this.toPay()
								}
							});
						} else {
							if (res.Msg == "当前订单已支付" || res.Msg == "当前订单无需支付" || res.Msg=='当前订单无需支付或已支付完成') {
								uni.showModal({
									title: res.Msg,
									showCancel: false,
									success(res) {
										if (res.confirm) uni.redirectTo({
											url: '../../pages/pay-success/pay-success?orderid=' + that.orderid
										});
									}
								});

							} else {
								uni.showModal({
									title: res.Msg,
									showCancel: false
								});
							}
						}
					})
					.catch(error => {
						console.error(error)

					});
			},
		}
	};
</script>

<style></style>
